import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;

/**
 * Created By  醉美柳舞之众星捧月
 *
 * @author 宋贤坤
 * @date 2019/6/14 10:21
 */
public class WXCore {

    private static final String APPID = "appid";
    private static final String WATERMARK = "watermark";

    /**
     * 解密数据
     *
     * @return
     * @throws Exception
     */
    public static String decrypt(String appId, String encryptedData, String sessionKey, String iv) {
        String result = "";
        try {
            AES aes = new AES();
            byte[] resultByte = aes.decrypt(Base64.decodeBase64(encryptedData), Base64.decodeBase64(sessionKey), Base64.decodeBase64(iv));
            if (null != resultByte && resultByte.length > 0) {
                result = new String(WxPKCS7Encoder.decode(resultByte));
                JSONObject jsonObject = JSONObject.fromObject(result);
                String decryptAppid = jsonObject.getJSONObject(WATERMARK).getString(APPID);
                if (!appId.equals(decryptAppid)) {
                    result = "";
                }
            }
        } catch (Exception e) {
            result = "";
            e.printStackTrace();
        }
        return result;
    }


    public static void main(String[] args) {

//        String appId = "wx4f4bc4dec97d474b";
//        String encryptedData = "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZMQmRzooG2xrDcvSnxIMXFufNstNGTyaGS9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+3hVbJSRgv+4lGOETKUQz6OYStslQ142dNCuabNPGBzlooOmB231qMM85d2/fV6ChevvXvQP8Hkue1poOFtnEtpyxVLW1zAo6/1Xx1COxFvrc2d7UL/lmHInNlxuacJXwu0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn/Hz7saL8xz+W//FRAUid1OksQaQx4CMs8LOddcQhULW4ucetDf96JcR3g0gfRK4PC7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns/8wR2SiRS7MNACwTyrGvt9ts8p12PKFdlqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYVoKlaRv85IfVunYzO0IKXsyl7JCUjCpoG20f0a04COwfneQAGGwd5oa+T8yO5hzuyDb/XcxxmK01EpqOyuxINew==";
//        String sessionKey = "tiihtNczf5v6AKRyjwEUhQ==";
//        String iv = "r7BXXKkLb8qrSNn05n0qiA==";

//        String appId = "wx8a4575c2cbfdb4b2";
//        String iv = "11FTibXTVPPaWzLGSg086A==";
//        String sessionKey = "Nwa44N7xSSdFiGdubFg3hw==";
//        String encryptedData = "bVUB5Hh6Z9cwD1e55B9onduF7/wY9u0+uBSlVxr9YdaZn9DSmhKNIahaqabo2eQrUvmaeHgBytEBjwH8Y7XeNi6ziqorNyBByeVU+CUgKQhW+5M4oUQeMDmKyi5tYsZYXynLXCdOwanisc86bF8IGCJ1hOsneIkHOxOk7Z7QfmGe9QVsRSLJ+AXriXwzZLMDudRCJowYWjPsUM+0mQjg2tJUSqWZxs2ko4rsc/PEx7330ESsvqmx5V3Q8/lUpkn1RIfGSnyZzYu24Kj+YAPA1uso1IZ6dpd2OZkyl+fRsZKwQFM7cLpFJlvvmOw1XYDGZ+VVsSTJ+wdrD1GaWnhYzSCQfxWXOGUs2qAAY9HTEU6P/Dz35NDIrASlJbO40a3QcTFrmGDF+4PInAZOunfx5G65PFhXCQaYAJA7dTcmgtK3ju8IcJTz7kPkHxiTV8FpaKaT0GhXSQXO0mTlBZJJZQWkBwT6P8c5eLaoUkhiCfU=";

        String appId = "wx6279ca8feaf2135d";
        String iv = "Yxmwj2+TvyjDZoX0xlEkFg==";
        String sessionKey = "w+F2iVKIWkwsvmOYFlmOCQ==";
        String encryptedData = "80VRzbi/5M1lpKeAwAnBQNZby5rnw1CA7MNxO4NdJVikCbDIyr/sJeuenzZX5JFQ9fhATifhCsvgBHcFW1psAAXKfpndiv4B6kStaJ6Zauz7Wk19DydSOKNSTjn10V+ZVF0xiFqJ4e3gwx1bPI+pqv+zQsNvZBFwWuG9e8wdwTUC9FjfnrIaEdz5G9kKbVekwR0n8ESvAN0U2W1SRkFuXasLbyAPZHf1rSFT7FsTmG9T1LLuIwcbS44n/QCM5e8b0dpRySR0zOlBCU/xePknr6u5wJkptATVhH28OuLjTqxlLQa8pob8BnHyjgxJ2w+GxHupvz0vl18e6hTk+kJ0uiSA/Bvcdae5ZiaTOQFY6btIe86LrPRanDPkOBU2p+2sVwXUfL/qDq1ihhXAOQjZXgIt+MTbTABQ8z8x4RgRmdZuRujXqEQ83/xxK6p8n9lxa92kB5wqi6uISkIlmxWCvRuSa4Z76aYaKa4d1YzJcbqh7MbDj0/WPvvYQ8RIQ2/0ojbPidJazCde4miaAoL2Og==";


        System.out.println(decrypt(appId, encryptedData, sessionKey, iv));
    }
}

